import React, { useEffect, useState } from 'react';
import CommonPage from '@/components/page/CommonPage';
import { useModel } from 'umi';
import { fetchOption } from '@/services/commCRUD';
import { format } from '@/utils/datetime';
const daoName = 'Fxgll';
const importName = 'Fxgll';
const columns: API.Column[] = [
  {
    title: '指标名称',
    width: 250,
    sorter: true,
    key: 'ZBNM',
    dataIndex: 'ZBNM',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '导入部门',
    width: 130,
    sorter: true,
    key: 'DPNM',
    dataIndex: 'DPNM',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '中小业务一部',
    width: 150,
    sorter: true,
    dataIndex: 'BM_211127',
    key: 'BM_211127',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '中小业务二部',
    width: 150,
    sorter: true,
    dataIndex: 'BM_211131',
    key: 'BM_211131',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '个贷业务一中心',
    width: 150,
    sorter: true,
    dataIndex: 'BM_211124',
    key: 'BM_211124',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '个贷业务二中心',
    width: 150,
    sorter: true,
    dataIndex: 'BM_211122',
    key: 'BM_211122',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '个贷业务三中心',
    width: 150,
    sorter: true,
    dataIndex: 'BM_211123',
    key: 'BM_211123',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '个贷业务四中心',
    width: 150,
    sorter: true,
    dataIndex: 'BM_211125',
    key: 'BM_211125',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '个贷业务五中心',
    width: 150,
    sorter: true,
    dataIndex: 'BM_211126',
    key: 'BM_211126',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '同业业务一部',
    width: 150,
    sorter: true,
    dataIndex: 'BM_221653',
    key: 'BM_221653',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '同业业务二部',
    width: 150,
    sorter: true,
    dataIndex: 'BM_221654',
    key: 'BM_221654',
    dispaly: true,
    filter: true,
    export: true,
  },
  {
    title: '同业业务三部',
    width: 150,
    sorter: true,
    dataIndex: 'BM_221655',
    key: 'BM_221655',
    dispaly: true,
    filter: true,
    export: true,
  },
];
const controlsImport: API.Control[] = [
  { name: 'SJBM', type: 'text', label: '涉及部门', grid: 12 / 24 },
  { name: 'ZBNM', type: 'text', label: '指标名称', grid: 12 / 24 },
  { name: 'FZQJ', type: 'text', label: '分值区间', grid: 12 / 24 },
  { name: 'JBF', type: 'text', label: '基本分270', grid: 12 / 24 },
  { name: 'BM_211127', type: 'text', label: '中小业务一部', grid: 12 / 24 },
  { name: 'BM_211131', type: 'text', label: '中小业务二部', grid: 12 / 24 },
  { name: 'BM_211124', type: 'text', label: '个贷业务一中心', grid: 12 / 24 },
  { name: 'BM_211122', type: 'text', label: '个贷业务二中心', grid: 12 / 24 },
  { name: 'BM_211123', type: 'text', label: '个贷业务三中心', grid: 12 / 24 },
  { name: 'BM_211125', type: 'text', label: '个贷业务四中心', grid: 12 / 24 },
  { name: 'BM_211126', type: 'text', label: '个贷业务五中心', grid: 12 / 24 },
  { name: 'BM_221653', type: 'text', label: '同业业务一部', grid: 12 / 24 },
  { name: 'BM_221654', type: 'text', label: '同业业务二部', grid: 12 / 24 },
  { name: 'BM_221655', type: 'text', label: '同业业务三部', grid: 12 / 24 },
];

const controls: API.Control[] = [
  { name: 'ZBNM', type: 'text', label: '指标名称', grid: 12 / 24 },
  { name: 'DPNM', type: 'text', label: '导入部门', grid: 12 / 24 },
  { name: 'BM_211127', type: 'text', label: '中小业务一部', grid: 12 / 24 },
  { name: 'BM_211131', type: 'text', label: '中小业务二部', grid: 12 / 24 },
  { name: 'BM_211124', type: 'text', label: '个贷业务一中心', grid: 12 / 24 },
  { name: 'BM_211122', type: 'text', label: '个贷业务二中心', grid: 12 / 24 },
  { name: 'BM_211123', type: 'text', label: '个贷业务三中心', grid: 12 / 24 },
  { name: 'BM_211125', type: 'text', label: '个贷业务四中心', grid: 12 / 24 },
  { name: 'BM_211126', type: 'text', label: '个贷业务五中心', grid: 12 / 24 },
  { name: 'BM_221653', type: 'text', label: '同业业务一部', grid: 12 / 24 },
  { name: 'BM_221654', type: 'text', label: '同业业务二部', grid: 12 / 24 },
  { name: 'BM_221655', type: 'text', label: '同业业务三部', grid: 12 / 24 },
];
const Fxgll: React.FC = () => {
  const { user } = useModel('@@initialState', (ret) => {
    return {
      user: ret.initialState?.user,
    };
  });
  const { permTypes } = useModel('sysInfo', (ret) => ({
    permTypes: ret.getCurPermTypes(),
  }));
  const [paras, setParas] = useState<Record<string, any>>({});
  let where: string = '';
  if (permTypes.indexOf('4') == -1) {
    where = `"CZR"=${user?.userID}`;
  }
  const currMonth = new Date().getMonth();
  const currQuarter = Math.floor(currMonth % 3 == 0 ? currMonth / 3 : currMonth / 3 + 1);
  let lastQuarterFirstDay: string;
  if (currQuarter == 1) {
    lastQuarterFirstDay = '2023-03-31';
  } else if (currQuarter == 2) {
    lastQuarterFirstDay = '2023-06-30';
  } else if (currQuarter == 3) {
    lastQuarterFirstDay = '2023-09-30';
  } else {
    lastQuarterFirstDay = '2023-12-31';
  }
  useEffect(() => {
    fetchOption('ShJhcwDcSetFXGL', where, [])
      .then(function (datas: Record<string, any>[]) {
        if (datas.length > 0) {
          const id = datas[0].BMID;
          setParas({
            DPID: id,
            DFJD: format(lastQuarterFirstDay, 'yyyy-MM-dd'),
          });
        }
      })
      .catch(function (ex) {
        console.error('错误', ex);
      });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  if (paras.DPID === undefined) {
    return <></>;
  }
  return (
    <CommonPage
      daoName={daoName}
      columns={columns}
      controls={controls}
      title="风险管理类"
      importName={importName}
      importControls={controlsImport}
      importParamNames={['DFJD', 'DPID']}
      importMode="DeleteAfterInsert"
      importTemplateName="风险管理类导入模板"
      onShortcutChange={async (vals) => setParas(vals)}
      importParamVals={{
        DFJD: paras.DFJD,
        DPID: '' + paras.DPID,
      }}
      keyColName="ID"
      shortcutFilters={{
        controls: [
          {
            name: 'DFJD',
            dataType: 'date',
            type: 'quarter',
            label: '选择季度',
            rules: [{ required: true, message: '季度不能为空' }],
            width: 120,
            initValue: paras.DFJD,
          },
          {
            name: 'DPID',
            label: '选择部门',
            type: 'select',
            width: 180,
            rules: [{ required: true, message: '部门不能为空' }],
            dataType: 'number',
            initValue: paras.DPID,
            fetch: {
              tableName: 'ShJhcwDcSetFXGL',
              optionTpl: 'BMMC',
              valueTpl: 'BMID',
              isDao: true,
              where: where,
            },
          },
        ],
      }}
    />
  );
};
export default Fxgll;
